<?php namespace Framework\Kernel;

class App
{
	public function run()
	{
		session_start();
		
		header("Content-type:text/html;charset=utf-8");
		
		spl_autoload_register([$this,'autoload']);
		set_error_handler(array($this,                      'error'), E_ALL);
        set_exception_handler(array($this,                  'exception'));
		//加载函数库
		require 'framework/Kernel/functions.php';

		$m = isset($_GET['m'])?$_GET['m']:'Home';
		$c = isset($_GET['c'])?$_GET['c']:'Index';
		$a = isset($_GET['a'])?$_GET['a']:'index';

		define('MODULE',$m);
		define('CONTROLLER',$c);
		define('ACTION',$a);
		define('IS_POST',!empty($_POST));

		//加载框架配置
		C(require 'framework/config.php');
		//加载公共配置
		C(require 'app/Common/Conf/config.php');
		//加载模块配置
		C(require 'app/'.$m.'/Conf/config.php');

		$class= $m.'\\Controller\\'.$c.'Controller';
		$obj = new $class;
		$obj->$a();
	}

	//类自动加载
	public function autoload($class)
	{
		$class = str_replace('\\', '/', $class);
		if(substr($class,0,9)=='Framework')
		{
			//加载框架内的类
			$file = $class.'.php';
		}
		else
		{	//加载项目类
			$file ='app/'.$class.'.php';
		}
		require_once($file);

	}

	//自定义异常理
    public function exception($e)
    {
    	$this->saveLog($e->getMessage());
    	if(DEBUG)
    	{
        	require 'framework/tpl/exception.php';
        }
        _404();
    }

    //错误处理
    public function error($errno, $error, $file, $line)
    { 
    	if(DEBUG)
    	{
        	require 'framework/tpl/error.html';
        }
        else
        {
        	//保存日志
        	$error =date("Y-m-d H:i:s")."File: $file; Error: $error".PHP_EOL;
        	$this->saveLog($error);
        }
        _404();
        
    }

    public function saveLog($message)
    {
    	$file = 'log/'.date('d').'.log';
        error_log($message, 3, $file,null);
    }
}















